//
//  JYRouterController.swift
//  JYRouter
//
//  Created by huangguojian on 2022/6/9.
//

import UIKit

///路由回调传参模型的抽象协议
public protocol JYRouterModelType {}

///路由跳转类型
public enum JYRouteStyle {
    case push, present, tabbarSelect, windowRoot
}

/// 路由在跳转时需要 remove 掉的controller方式
public enum JYRouterWillRemoveControllersType {
    /// remove 当前所有
    case all
    /// 包含以下类型的 Controller 都remove 掉
    case include(vcTypes: [UIViewController.Type])
    /// 除以下类型的 Controller，其他 都remove 掉
    case exclude(vcTypes: [UIViewController.Type])
    /// 不remove
    case none
}

///路由跳转的Controller的遵循协议
public protocol JYRoutable: UIViewController {
    ///路由跳转类型
    ///
    var routeStyle: JYRouteStyle { get }
    ///通过路由回调传参
    var routeCallBack: ((JYRouterModelType) -> Void)? { get set }
}

extension JYRoutable {
    ///路由返回
    public func routeBack(_ animated: Bool = true, completion: (() -> Void)? = nil) {
        switch self.routeStyle {
        case .push:
            self.navigationController?.popViewController(animated: animated)
        case .present:
            if let vc = self.presentingViewController {
                vc.dismiss(animated: animated, completion: completion)
            } else {
                self.dismiss(animated: animated, completion: completion)
            }
        case .tabbarSelect, .windowRoot:
            break
        }
    }
}
